const app = getApp();
const { request } = require('../api.js'); 

Page({
  data: {
    userInfo: {},
    dynamicList: [],
    page: 1,
    hasMore: true,
    loading: false,
    id:0,
  },

  onLoad(options) {
    const id = options.id;
    this.setData({
        id: id
    });
    this.loadUserInfo(id);

  },

  // 加载用户信息
  loadUserInfo(id,isRefresh) {
    if (this.data.loading) return;
    this.setData({ loading: true });
    var _this = this

    request({
        url: app.globalData.baseUrl + 'public/profile/' + id,
        data: { 
            page: _this.data.page,
        }
    }).then(res => {
        const list = res.data.data.trends || []
            const newList = isRefresh ? list : _this.data.dynamicList.concat(list)
            this.setData({
                userInfo: res.data.data.needs,
                dynamicList: newList,
                hasMore: list.length === 5,
                isLoading: false
            })
    })
    .finally(() => {
        _this.setData({
            loading: false,
        })
    });
    return
    // 模拟API调用
    setTimeout(() => {
      const mockUserInfo = {
        id: id,
        nickname: '温柔如风',
        age: 58,
        city: '北京市朝阳区',
        income: '月收入8000+',
        description: '退休教师，喜欢读书、旅游，希望找一个志同道合的伴侣共度晚年。性格温和，生活规律，喜欢安静的环境。',
        avatar: '/images/avatar/11.jpg',
        datingType: 'partner'
      };
      
      this.setData({
        userInfo: mockUserInfo
      });
    }, 500);
  },

  // 加载更多
  loadMore() {
    if (!this.data.hasMore || this.data.loading) return;
    
    this.setData({
      page: this.data.page + 1
    });
    
    this.loadUserInfo(this.data.id);
  },

   // 查看动态详情
  viewDynamic(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/dynamic-detail/dynamic-detail?id=${id}`
    });
  },

  // 查看用户资料
  viewUserProfile(e) {
    const userId = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: `/pages/user-detail/user-detail?id=${userId}`
    });
  },

    // 上拉加载
    onReachBottom() {
        if (this.data.hasMore && !this.data.loading) {
          this.loadMore();
        }
    },

  // 打招呼
  sayHello() {
    if (!app.globalData.isLogin) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/register/register'
            });
          }
        }
      });
      return;
    }

    let _this = this
    request({
        url: app.globalData.baseUrl + 'api/chat-call?obj_user_id='+this.data.id,
    }).then(res => {
         // 更新该用户的打招呼状态
        let userInfo = _this.data.userInfo;
        userInfo.is_called = true; // 标记为已打招呼
        _this.setData({
            userInfo: userInfo,
        });
        wx.showToast({
            title: "已打招呼，等TA回应",
            icon: 'success'
        })
    })
  },

  // 开始聊天
  startChat() {
    if (!app.globalData.isLogin) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/register/register'
            });
          }
        }
      });
      return;
    }
    var _this = this
    request({
        url: app.globalData.baseUrl + 'api/chat-roomid?obj_user_id='+_this.data.id,
    }).then(res => {
        wx.navigateTo({
            url: `/pages/chat/chat?room_id=${res.data.data.room.id}`
         });
    })
   
  }
}); 